package com.cskaoyan.mall.service;

import com.cskaoyan.mall.bean.Ad;
import com.cskaoyan.mall.bean.AdExample;
import com.cskaoyan.mall.mapper.AdMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdServiceImpl implements AdService {
    @Autowired
    AdMapper adMapper;
    @Override
    public List<Ad> getAdList(Integer page, Integer limit, String sort, String order,String name,String content) {
        PageHelper.startPage(page,limit);
        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();
        if (name != null){
            criteria.andNameLike("%" + name + "%");
        }
        if (content != null){
            criteria.andContentLike("%" + content + "%");
        }
        adExample.setOrderByClause(sort + " " + order);
        List<Ad> ads = adMapper.selectByExample(adExample);
        PageInfo<Ad> userPageInfo = new PageInfo<Ad>(ads);
        return ads;
    }

    @Override
    public void deleteAdById(Integer id) {
        adMapper.deleteAdById(id);
    }

    @Override
    public Ad updateAd(Ad ad) {
        int i = adMapper.updateByPrimaryKey(ad);
        return adMapper.selectByPrimaryKey(ad.getId());
    }

    @Override
    public Ad createAd(Ad ad) {
        int insert = adMapper.insert(ad);
        Ad adVo = adMapper.selectLastInsertAd();
        return adVo;
    }
}
